﻿using System;
using System.Windows;
using System.Windows.Controls;
using DiagramDesigner.BusinessModelObject;
using DiagramDesigner.Controls;
using AvalonDock;
using System.Collections;
using System.Collections.Generic;

using System.Globalization;
using System.IO;
using System.Linq;
using System.Xml;
using System.Xml.Linq;

namespace DiagramDesigner
{
    public partial class Window1 : Window
    {
        #region Variables
        private string route = String.Empty;
        private string XMLFileName = String.Empty;
        private XElement XMLContent;
        #endregion

        public Window1()
        {
            InitializeComponent();
            //bodyXXX.Focus();
            MyDesigner.Focus();
        }

        //public UCProperties PropertiesPanel
        //{
        //    get { return ucProperties; }
        //}

        //void showProperty()
        //{
        //    LevelModel l = new LevelModel();

        //    NivelLifespan n = new NivelLifespan { Name = "nivelUno", TemporalSupport = TypeNivelTemporalSupport.Interval };
        //    l.Nivel = n;

        //    AtributoNoEspTemp a = new AtributoNoEspTemp();
        //    a.Name = "Atributo Uno";
        //    a.TemporalSupport = TypeAtributoTemporalSupport.Instant;
        //    a.Key = TypeKey.Y;

        //    a.DataType = new TypeData{
        //                    TipoDato = TypeDatos.Number,
        //                    Precision = 2,
        //                    Escala = 18};
        //    l.Atributos.Add(a);

        //    ucProperties.ShowProperties<LevelModel, AtributoNoEspTemp>(l, (AtributoNoEspTemp)l.Atributos[0]);
        //}

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //showProperty();
        }

        private void bodyXXX_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            #region Save
            int index = 0;
            string title = String.Empty;
            DocumentPane dp = (DocumentPane)documentModelsXXX;
            foreach (DocumentModel dmNew in dp.Items)
            {
                if (dmNew.IsActiveDocument)
                {
                    index = dp.SelectedIndex;
                    GroupBox gbNew = (GroupBox)dmNew.Content;
                    ScrollViewer svNew = (ScrollViewer)gbNew.Content;
                    //dcNew = (DesignerCanvas)svNew.Content;
                    title = dmNew.Title;
                    break;
                }
            }

            IEnumerable<DesignerItem> designerItems = MyDesigner.Children.OfType<DesignerItem>();
            IEnumerable<Connection> connections = MyDesigner.Children.OfType<Connection>();

            XElement designerItemsXML = MyDesigner.SerializeDesignerItems(designerItems);
            XElement connectionsXML = MyDesigner.SerializeConnections(connections);

            XElement root = new XElement("Root");
            root.Add(designerItemsXML);
            root.Add(connectionsXML);

            if (!XMLFileName.Equals(""))
            {
                root.AddFirst(new XElement("FileName", XMLFileName));
                if (XMLContent.Value.Equals(root.Value))
                {
                    return;
                }
                else
                {
                    MyDesigner.SaveFile(root, route, title);
                }
            }
            else
            {
                string fileName = MyDesigner.SaveAsFile(root, title);
                if (!fileName.Equals(String.Empty))
                {
                    ((DocumentModel)dp.Items[index]).Title = fileName;
                    XMLContent = root;
                    XMLFileName = fileName;
                }
                else
                {
                    return;
                }
            }
            #endregion

            var win = (Window1)Window.GetWindow(this);
            foreach (DocumentModel dmNew in dp.Items)
            {
                if (dmNew.IsActiveDocument)
                {
                    index = dp.SelectedIndex;
                    dp.Items.Remove(dmNew);
                    XMLFileName = String.Empty;
                    XMLContent = null;
                    if (dp.Items.Count == 0)
                    {
                        win.Close();
                        return;
                    }
                }
            }
        }

        private void bodyXXX_Closed(object sender, System.EventArgs e)
        {
            int a = 1;
            int b = 1;
            int c = 1;
            c = a + b;
            return;
        }

        private void bodyXXX_Loaded(object sender, RoutedEventArgs e)
        {
            int a = 1;
            int b = 1;
            int c = 1;
            c = a + b;
        }

        private void wMeasure_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            //MyDesigner.Exit_Executed(sender, e);


            //e.Cancel = true;
            //if (MyDesigner.Exit_Executed(sender, e))
            //{
            //    this.Close();
            //}
            //#region Closing Form
            //DocumentModel dm = (DocumentModel)bodyXXX;
            //DocumentPane dp = (DocumentPane)documentModelsXXX;
            //ResizingPanel rp = (ResizingPanel)dp.Parent;
            //DockingManager dkm = (DockingManager)rp.Parent;
            //Grid grid_1 = (Grid)dkm.Parent;
            //Grid grid_2 = (Grid)grid_1.Parent;

            //DesignerCanvas dcNew = new DesignerCanvas();

            //if (MessageBoxResult.Yes == MessageBox.Show("Save changes?", "CSTMTool", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.Yes))
            //{
            //    GroupBox gbNew = (GroupBox)dm.Content;
            //    ScrollViewer svNew = (ScrollViewer)gbNew.Content;
            //    dcNew = (DesignerCanvas)svNew.Content;

            //    IEnumerable<DesignerItem> designerItems = dcNew.Children.OfType<DesignerItem>();
            //    IEnumerable<Connection> connections = dcNew.Children.OfType<Connection>();

            //    DesignerCanvas designerCanvas = new DesignerCanvas();

            //    XElement designerItemsXML = designerCanvas.SerializeDesignerItems(designerItems);
            //    XElement connectionsXML = designerCanvas.SerializeConnections(connections);

            //    XElement root = new XElement("Root");
            //    root.Add(designerItemsXML);
            //    root.Add(connectionsXML);

            //    string routeSave = designerCanvas.SaveAsFile(root);

            //    if (routeSave.Equals("") || routeSave.Equals("File1.xml"))
            //    {
            //        return;
            //    }
            //    else
            //    {
            //        dp.Items.Remove(dm);
            //        MessageBox.Show("Schema saved successfully.", "CSTMTool", MessageBoxButton.OK, MessageBoxImage.Information);
            //    }
            //}
            //else
            //{
            //    // Si es el único documento y no deseo grabarlo, cierro la aplicación
            //    if (dp.Items.Count == 1)
            //    {
            //        dp.Items.Remove(dm);
            //        Window window = (Window)grid_2.Parent;
            //        //window.Close();
            //    }
            //    else
            //    {
            //        // Elimino el documento
            //        //MessageBox.Show("La información se ha guardado correctamente!", "Aviso", MessageBoxButton.OK, MessageBoxImage.Information);
            //        dp.Items.Remove(dm);
            //    }
            //}
            //#endregion
        }
    }
}